<template>
	<div style="width: 100%;height: 100%">
		<el-row style="height: 10%;">
			<el-form class="search-form form-inline" label-width="120px" size="mini" ref="searchForm">
				<el-col :span="8">
					<el-form-item class="form-group" label="用户名：">
						<el-input v-model="searchForm.username" placeholder="" clearable />
					</el-form-item>
				</el-col>
				<el-col :span="8">
					<el-form-item class="form-group" label="性别：">
						<el-select v-model="searchForm.sex" clearable placeholder="请选择">
							<el-option v-for="item in sexEnum" :key="item.value" :label="item.label"
								:value="item.value">
							</el-option>
						</el-select>
					</el-form-item>
				</el-col>
			</el-form>
		</el-row>
		<el-row style="height: 10%;">
			<el-button type="primary" size="mini" plain class="el-icon-search" @click="doSearch(1)">查询</el-button>
			<el-button type="primary" size="mini" plain class="el-icon-edit" @click="doUpdate()">修改</el-button>
			<el-button type="primary" size="mini" plain class="el-icon-circle-close" @click="doDelate()">删除</el-button>
		</el-row>
		<el-row style="height: 75%;">
			<el-table ref="table" :data="tableData" style="position: absolute" height="100%" size="mini"
				highlight-current-row border @current-change="handleCurrentRow"
				@selection-change="handleSelectionChange">
				<el-table-column type="selection" width="60" align="center" />
				<el-table-column prop="username" align="center" label="姓名" width="150" />
				<el-table-column prop="sex" align="center" label="性别" width="150" />
				<el-table-column prop="username" align="center" label="账号" width="200" />
				<el-table-column prop="password" align="center" label="密码" width="200" />
				<el-table-column prop="phone" align="center" label="联系方式" width="200" />
				<el-table-column prop="cardId" align="center" label="身份证号" :min-width="120" />
			</el-table>
		</el-row>
		<el-row style="height: 5%;">
			<el-pagination :page-sizes="[30, 50, 100]" :current-page="searchForm.pageNo"
				:page-size="searchForm.pageSize" layout="total, sizes, prev, pager, next, jumper" :total="totalCount"
				@size-change="handleSizeChange" @current-change="handleCurrentChange" />
		</el-row>

		<el-dialog title="修改用户信息" :visible.sync="dialogVisible" width="60%" :before-close="handleClose">
			<el-form class="search-form form-inline" :model="info" :rules="rules" label-width="120px" size="mini"
				ref="info">
				<el-row>
					<el-col :span="12">
						<el-form-item class="form-group" label="姓名：" prop="username">
							<el-input v-model="info.username" />
						</el-form-item>
					</el-col>
					<el-col :span="12">
						<el-form-item class="form-group" label="性别" prop="sex">
							<el-select v-model="info.sex" clearable placeholder="请选择">
								<el-option v-for="item in sexEnum" :key="item.value" :label="item.label"
									:value="item.value">
								</el-option>
							</el-select>
						</el-form-item>
					</el-col>
				</el-row>
				<el-row>
					<el-col :span="12">
						<el-form-item class="form-group" label="账号：" prop="account">
							<el-input v-model="info.account" />
						</el-form-item>
					</el-col>
					<el-col :span="12">
						<el-form-item class="form-group" label="密码：" prop="password">
							<el-input v-model="info.password" />
						</el-form-item>
					</el-col>
				</el-row>
				<el-row>
					<el-col :span="12">
						<el-form-item class="form-group" label="联系方式：" prop="phone">
							<el-input v-model="info.phone" />
						</el-form-item>
					</el-col>
					<el-col :span="12">
						<el-form-item class="form-group" label="身份证号" prop="cardId">
							<el-input v-model="info.cardId" />
						</el-form-item>
					</el-col>
				</el-row>
			</el-form>
			<span slot="footer" class="dialog-footer">
				<el-button @click="dialogVisible = false">取 消</el-button>
				<el-button type="primary" @click="save">确 定</el-button>
			</span>
		</el-dialog>
	</div>
</template>

<script>
	const validateCardId = (rule, value, callback) => {
		if (value) {
			const rege =
				/^[1-9]\d{5}(?:18|19|20)\d{2}(?:0[1-9]|10|11|12)(?:0[1-9]|[1-2]\d|30|31)\d{3}[\dXx]$/
			if (!rege.test(value)) {
				callback(new Error('请输入正确的身份证号码'))
			} else {
				callback()
			}
		} else {
			callback(new Error('请输入身份证号码'))
		}
	}

	const validatePhone = (rule, value, callback) => {
		if (value) {
			const rege =
				/^(?:(?:\+|00)86)?1(?:(?:3[\d])|(?:4[5-79])|(?:5[0-35-9])|(?:6[5-7])|(?:7[0-8])|(?:8[\d])|(?:9[189]))\d{8}$/
			if (!rege.test(value)) {
				callback(new Error('请输入正确的手机号码'))
			} else {
				callback()
			}
		} else {
			callback(new Error('请输入手机号码'))
		}
	}
	export default {
		data() {
			return {
				searchForm: {
					username: '',
					sex: '',
					pageNo: 1,
					pageSize: 30
				},
				sexEnum: [{
					value: '女',
					label: '女'
				}, {
					value: '男',
					label: '男'
				}],
				tableData: [],
				totalCount: 0,
				dialogVisible: false,
				selection: [],
				info: {},
				rules: {
					sex: {
						required: true,
						message: "请选择性别",
						trigger: "change"
					},
					username: [{
							required: true,
							message: '请输入用户名',
							trigger: 'blur'
						},
						{
							min: 2,
							max: 5,
							message: '长度在 2 到 5 个字符',
							trigger: 'blur'
						}
					],
					account: [{
							required: true,
							message: '请输入账号',
							trigger: 'blur'
						},
						{
							min: 6,
							max: 10,
							message: '长度在 6 到 10 个字符',
							trigger: 'blur'
						}
					],
					password: [{
							required: true,
							message: '请输入密码',
							trigger: 'blur'
						},
						{
							min: 8,
							max: 16,
							message: '密码长度在 8 到 16 个字符',
							trigger: 'blur'
						}
					],
					phone: [{
						required: true,
						validator: validatePhone,
						trigger: 'blur'
					}],
					cardId: [{
						required: true,
						validator: validateCardId,
						trigger: 'blur'
					}],
				}
			}
		},
		methods: {
			doSearch(pageNo) {
				this.searchForm.pageNo = pageNo
				this.$axios.post("http://localhost:8888/getUser/Page?pageNum=" + this.searchForm.pageNo +
						"&pageSize=" + this.searchForm.pageSize, this.searchForm)
					.then((data) => {
						console.log(data)
						this.tableData = data.data.records
						this.totalCount = data.data.total
					})
			},
			handleCurrentChange(val) {
				this.doSearch(val)
			},
			handleSizeChange(val) {
				this.searchForm.pageSize = val
				this.doSearch(1)
			},
			handleSelectionChange(selection) {
				this.selection = selection
			},
			doUpdate() {
				console.log(this.selection)
				if (this.selection.length == 1) {
					this.info = this.selection[0]
					this.dialogVisible = true
				} else if (this.selection.length == 0) {
					this.$message.warning('请选择一条信息进行修改');
				} else {
					this.$message.warning('只能选择一条信息进行修改');
				}
			},
			save() {
				this.$refs.info.validate((valid) => {
					if (valid) {
						this.$axios.post("http://localhost:8888/user/update", this.info).then((data) => {
							if (data.data == 1) {
								this.dialogVisible = false
								this.$message.success('修改成功');
								this.doSearch(1)
							}
						})
					}
				})
			},
			doDelate() {
				if (this.selection.length == 0) {
					this.$message.warning('请选择一条信息进行修改');
				} else {
					let ids = []
					this.selection.forEach((item) => {
						ids.push(item.id)
					})
					this.$axios.post("http://localhost:8888/user/delete?ids=" + ids.join(",")).then((data) => {
						if (data.data >= 1) {
							this.$message.success('删除成功');
							this.doSearch(1)
						}
					})
				}
			},

		},
		mounted() {
			this.doSearch(1)
		}
	}
</script>

<style scoped>
	.el-select {
		display: flex;
	}
</style>
